package ws

import (
	"time"
)

type Err int

const (
	ErrNone 					Err 	= 0
	ErrParameter 				Err 	= -1001
	ErrGetDataFailed 			Err 	= -1002
	ErrUserNotExists 			Err 	= -1003
	ErrCoinNotExists 			Err 	= -1100
	ErrExchangeNotExists 		Err  	= -1200
	ErrInnerError 				Err 	= -1500
	ErrUnknownError 			Err 	= -9999
)

func (e Err) ShowMessage() string {
	errMap := map[Err]string{
		0: 				"",
		-1001: 			"Parameter error",
		-1002: 			"Get data failed",
		-1003: 			"User not exists",

		-1100: 			"Coin not exists",			// 1100 开头的与币种有关

		-1200: 			"Exchange not exists",		// 1200 开头的与交易所相关

		-1500: 			"Inner error",

		-9999: 			"Unknown error",
	}

	if msg, ok := errMap[e]; ok {
		return msg
	}
	return "Unknown error"
}

func ErrorMsg(code Err, messageId int) interface{} {
	type errorResponse struct {
		CommonResponse
		Data 		interface{}			`json:"data"`
	}

	var resp errorResponse
	resp.MessageId = messageId
	resp.Timestamp = time.Now().Unix()
	resp.ErrorCode = code
	resp.ErrorMessage = code.ShowMessage()
	resp.Data = nil
	return resp
}

func WsResponse(m interface{}, messageId int) interface{} {
	type errorResponse struct {
		CommonResponse
		Data 		interface{}			`json:"data"`
	}

	var resp errorResponse
	resp.MessageId = messageId
	resp.Timestamp = time.Now().Unix()
	resp.ErrorCode = ErrNone
	resp.ErrorMessage = ErrNone.ShowMessage()
	resp.Data = m
	return resp
}